/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-27     yoyoyes      first version
 */

#include "thread_create_demo.h"

static rt_thread_t tid1 = RT_NULL;          /* 线程控制块指针 */

/*
 * @brief  线程1入口函数
 *
 * @param  parameter 线程入口函数参数
 *
 * @return None
 */
static void thread1_entry(void *parameter)
{
    rt_uint32_t count = 0;

    while(1)
    {
        rt_kprintf("thread1 count: %d\n", count++);
        rt_thread_mdelay(2000);
    }
}

ALIGN(RT_ALIGN_SIZE)                        /* 系统对齐 */
static char thread2_stack[1024];            /* 线程栈 */
static struct rt_thread thread2;            /* 线程控制块 */

/*
 * @brief  线程2入口函数
 *
 * @param  parameter 线程入口函数参数
 *
 * @return None
 */
static void thread2_entry(void *parameter)
{
    rt_uint32_t count = 0;

    for(count = 0; count < 10; count++)
    {
        rt_kprintf("thread2 count: %d\n", count);
    }

    rt_kprintf("thread2 exit\n");
}

/*
 * @brief  线程创建Demo函数
 *
 * @param  None
 *
 * @return None
 */
void thread_create_demo(void)
{
    /* 创建线程1（动态） */
    tid1 = rt_thread_create("thread1", thread1_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    /* 启动线程1 */
    if(tid1 != RT_NULL)
    {
        rt_thread_startup(tid1);
    }

    /* 初始化线程2（静态） */
    rt_thread_init(&thread2, "thread2", thread2_entry, RT_NULL, &thread2_stack[0], sizeof(thread2_stack), THREAD_PRIORITY - 1, THREAD_TIMESLICE);
    /* 启动线程2 */
    rt_thread_startup(&thread2);
}
